// pages/work-order/index/index.ts
const app = getApp()
Page({
  data: {
    statusBarHeight: 0,
    navBarHeight: 0,
    topBg: app.getImgUrl('workOrder.webp'),
    cateList: [
      { id: 0, name: '执行中', value: 10, querystatus: 4 },
      { id: 1, name: '未指派', value: 1, querystatus: 2 },
      { id: 2, name: '超时单', value: 2, querystatus: 3 },
      { id: 3, name: '已完成', value: 1, querystatus: 6 },
      { id: 4, name: '待办', value: 5, querystatus: 1 },
    ],
    cateActiveIndex: 0,
    currentPage: 1,
    orderList: [
      // 工单ITEM 模板
      // {
      //   date: "2025年3月7日星期五",
      //   address: "西昌西路11号大厦35号",
      //   distance: "3.3KM",
      //   remainingTime: "3天",
      //   departureTime: "10:00前",
      //   type: "故障维修",
      //   shortType: "故障",
      //   description: "故障：设备检测到交换机与xx通讯提……",
      //   creator: "陈灵云",
      //   executor: "奕享 189-1088-9999",
      //   steps: ["已出发", "开始处理", "维修报价", "已完结"],
      //   currentStep: 3,
      //   dayName: '今天',
      //   iconList: ['icon-yinliang1', 'icon-play-filled', 'icon-image-fill', 'icon-qubiezhen']
      // }
    ]
  },
  onLoad() {
    // 同步获取窗口信息
    const windowInfo = wx.getWindowInfo();
    const statusBarHeight = windowInfo.statusBarHeight;
    const navBarHeight = statusBarHeight + 44;
    this.setData({ statusBarHeight, navBarHeight });
    this.getWorkOrderList(4)
  },
  switchCateActive(e) {
    const item = e.currentTarget.dataset.item; // 获取点击项的id
    this.setData({
      cateActiveIndex: item.id // 更新激活索引
    });
    this.getWorkOrderList(item.querystatus);
  },
  getWorkOrderList(status) {
    let params = {
      op: "ajaxWorkOrderList",
      page: this.data.currentPage,
    }
    if (status) {
      params.querystatus = status
    }
    app.axios('workOrder', params).then(res => {
      if (res.code) {
        const orderList = []
        if (res.lists && res.lists.length > 0) {
          const iconDict = {
            imgs: 'icon-image-fill',
            videos: 'icon-play-filled',
            files: 'icon-qubiezhen'
          }
          res.lists.forEach(item => {
            // 为每个工单生成独立的图标列表
            const iconList = []
            // 按顺序检查媒体类型字段
            if (item.imgs && item.imgs !== '') {
              iconList.push(iconDict.imgs)
            }
            if (item.videos && item.videos !== '') {
              iconList.push(iconDict.videos)
            }
            if (item.files && item.files !== '') {
              iconList.push(iconDict.files)
            }
            
            // 构造工单对象
            const orderItem = {
              ...item,
              shortType: this.getShortType(item.type),
              address: item.zaddr,
              date: app.dateFormatter(item.addtime * 1000),
              creator: item.crname,
              executor: item.exuserid,
              remaining: this.formatTimeLeft(item.ftime),
              iconList: iconList // 注入生成的图标列表
            }
            orderList.push(orderItem)
          })
        }
        this.setData({
          orderList: orderList
        })
      }
    })
  },
  getShortType(type) {
    const typeNum = parseInt(type, 10); // 安全转换为整数
    switch (typeNum) {
      case 1:
        return '项目';
      case 2:
        return '客户';
      case 3:
      case 4:
        return '维修';
      case 6:
        return '设备';
      default:
        return ''; // 非指定类型返回空字符串
    }
  },
  formatTimeLeft(timestamp) {
    const target = timestamp * 1000; // 转为毫秒时间戳
    const now = Date.now();

    // 过期判断
    if (target <= now) return "已过期";

    // 时间差计算
    const diff = target - now;
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));

    // 动态构建结果片段
    const parts = [];
    if (days > 0) parts.push(`${days}天`);
    if (hours > 0) parts.push(`${hours}小时`);
    if (minutes > 0) parts.push(`${minutes}分`);

    // 处理剩余时间不足1分钟的情况
    if (parts.length === 0) return "0分";

    return parts.join('');
  },
  navigateTo(e) {
    app.navigateTo(e.currentTarget.dataset.url, e.currentTarget.dataset.type)
  }
})